﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sinacor.OMM.Receptor.CommunicationChannel
{
    public class CommunicationChannelBase
    {

        private OMM.Configuration.Receptor.CommunicationChannel _communicationChannelConfiguration = null;
        private OMM.ConnectionManager.MSMQ.MsmqSender _mainRepositorySender = null;
        private OMM.ConnectionManager.MSMQ.MsmqReceiver _mainRepositoryReceiver = null;
        

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="communicationChannel"></param>
        protected CommunicationChannelBase(OMM.Configuration.Receptor.CommunicationChannel communicationChannel)
        {
            _communicationChannelConfiguration = communicationChannel;
            this.CreateRepositories();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Communication Channel Configuration
        /// </summary>
        protected OMM.Configuration.Receptor.CommunicationChannel CommunicationChannelConfiguration
        {
            get
            {
                return _communicationChannelConfiguration;
            }
        }

        /// <summary>
        /// Main Repository Message 
        /// </summary>
        protected OMM.ConnectionManager.MSMQ.MsmqSender MainRepositoryMessage
        {
            get { return _mainRepositorySender; }
        }

        /// <summary>
        /// Contingency Repository Receiver
        /// </summary>
        protected OMM.ConnectionManager.MSMQ.MsmqReceiver MainRepositoryReceiver
        {
            get { return _mainRepositoryReceiver; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Create main and contingency repository
        /// </summary>
        private void CreateRepositories()
        {
            //
            OMM.ConnectionManager.MSMQ.MSMQHelper _helper = new Sinacor.OMM.ConnectionManager.MSMQ.MSMQHelper();

            //
            //_communicationChannelConfiguration.MessageMainRepository.ConnectionInformation["CONNECTION_STRING"] = @"FormatName:Direct=OS:wspdbb1881\Private$\stm";
            _mainRepositorySender = _helper.CreateRepository(_communicationChannelConfiguration.MessageMainRepository);

            if (_communicationChannelConfiguration.SystemMessageRepository != null)
            {
                _mainRepositoryReceiver = _helper.CreateRepositoryMsmqReceiver(_communicationChannelConfiguration.SystemMessageRepository);
                _mainRepositoryReceiver.MessageReceived += new Sinacor.OMM.ConnectionManager.MSMQ.MsmqReceiver.MessageArrival(_mainRepositoryReceiver_MessageReceived);
            }
        }

        void _mainRepositoryReceiver_MessageReceived(System.Messaging.Message msg, PSE.Framework.Messaging.MessageReceivedEventArgs e)
        {
            e.Result = MessageReceiverArrival(msg);
        }

        protected virtual bool MessageReceiverArrival(System.Messaging.Message message)
        {
            return true;
        }
        

        #endregion

    }
}
